import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:photo_assistant/view/camera/continuous_camera_controller.dart';

class CameraPage extends StatelessWidget {
  const CameraPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: GetBuilder<ContinuousCameraController>(
        builder: (ContinuousCameraController controller) {
          if (controller.cameras.isEmpty) {
            return const Center(child: Text("没有摄像头"));
          } else {
            return Column(
              children: [
                Expanded(
                  child: CameraPreview(controller.controller),
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    TextButton(
                      child: const Text('拍照'),
                      onPressed: () => controller.takePictures(),
                    ),
                    TextButton(
                      child: const Text('返回'),
                      onPressed: () => Get.back(result: controller.pictures),
                    ),
                  ],
                ),
                Expanded(
                  child: GridView.builder(
                    gridDelegate:
                        const SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 3,
                      crossAxisSpacing: 10,
                      mainAxisSpacing: 10,
                    ),
                    itemCount: controller.pictures.length,
                    itemBuilder: (context, index) {
                      return Image.file(controller.pictures[index]);
                    },
                  ),
                ),
              ],
            );
          }
        },
      ),
    );
  }
}
